<?php
Class User_model extends CI_Model
{
	function login($username, $password)
	{
		$this -> db -> select('username, password, nama, mail, level');
		$this -> db -> from('users');
		$this -> db -> where('username', $username);
		$this -> db -> where('password', $password);
		$this -> db -> limit(1);

		$query = $this -> db -> get();

		if($query -> num_rows() == 1)
		{
			return $query->result();
		}
		else
		{
			return false;
		}
	}
	
	function get($username = null) 
	{
		$this->db->select()->from('users');
		if ($username != null) 
		{
			$this->db->where('username', $username);
		}
		else 
		{
			$this->db->order_by('username');
		}
 
		$query = $this->db->get();
 
		if ($username != null) 
		{
			return $query->row_array();
		}
		else 
		{
			return $query->result_array();
		}
	}
	
	public function is_user_exist($username)
	{
		$query = $this->db->get_where('users', array('username' => $username));
		if($query->num_rows() > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public function update($username,$data)
	{
		$this->db->where('username', $username);
		$this->db->update('users', $data); 
	}
	
	public function insert($data)
	{
		if($this->db->insert('users', $data))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	public function delete($username)
	{
		$this->db->delete('users', array('username' => $username) ); 
	}
}
?>